package work.shyk.gaas;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtils {

	private static final ThreadLocal<Calendar> calendarThreadLocal = ThreadLocal
			.withInitial(() -> Calendar.getInstance());

    private static final ThreadLocal<SimpleDateFormat> TIME_HOUR_PARESER = ThreadLocal
            .withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH"));

	/**
	 * 判断目标时间是否在开始时间与结束时间之间，用大于等于和小于等于判定
	 *
	 * @param timeInMillis
	 *            待转换的时间，以unix纪元开始的毫秒数
	 * @param startAsInt
	 *            开始时间，仅包含小时和分钟的整数，如时分秒为'08:03:50'的时间，其小时和分钟的整数应为'803'
	 * @param endAsInt
	 *            结束时间，仅包含小时和分钟的整数，如时分秒为'23:59:50'的时间，其小时和分钟的整数应为'2359'
	 * @return 如果目标时间的小时和分钟落在开始与结束时间之内，则返回true，否则返回false
	 */
	public static boolean betweenWithhourAndMinute(long timeInMillis, int startAsInt, int endAsInt) {
		int targetAsInt = toHourAndMinuteAsInt(timeInMillis);
		return targetAsInt >= startAsInt && targetAsInt <= endAsInt;
	}

	/**
	 * 将毫秒数转换成只包含小时与分钟的整数形式，如时分秒为'08:03:50'的时间，其小时和分钟的整数应为'803'
	 *
	 * @param timeInMillis
	 *            以unix纪元开始的毫秒数
	 * @return 小时与分钟的整数形式
	 */
	private static int toHourAndMinuteAsInt(long timeInMillis) {
		Calendar calendar = calendarThreadLocal.get();
		calendar.setTimeInMillis(timeInMillis);
		return calendar.get(Calendar.HOUR_OF_DAY) * 100 + calendar.get(Calendar.MINUTE);
	}

	/**
	 * 获得指定日期的前一天
	 *
	 * @param specifiedDay
	 * @return
	 * @throws Exception
	 */
	public static String getSpecifiedDayBefore(String specifiedDay) {
		// SimpleDateFormat simpleDateFormat = new
		// SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();
		Date date = null;
		try {
			date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		c.setTime(date);
		int day = c.get(Calendar.DATE);
		c.set(Calendar.DATE, day - 1);

		String dayBefore = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
		return dayBefore;
	}

	/**
	 * 获得指定日期的后一天
	 *
	 * @param specifiedDay
	 * @return
	 */
	public static String getSpecifiedDayAfter(String specifiedDay) {
		Calendar c = Calendar.getInstance();
		Date date = null;
		try {
			date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		c.setTime(date);
		int day = c.get(Calendar.DATE);
		c.set(Calendar.DATE, day + 1);

		String dayAfter = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
		return dayAfter;
	}

	/**
	 * 根据 传输过来的参数 获得指定的分钟
	 *
	 * @param currentTime
	 * @param plusMinutes
	 * @return
	 */
	public static String getMinuteChanges(Date currentTime, int plusMinutes){
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Calendar c = new GregorianCalendar();
		c.setTime(currentTime);
		c.add(Calendar.MINUTE,plusMinutes);
		currentTime=c.getTime();
		String changeMinute = df.format(currentTime);
		return changeMinute;
	}
	/**
	 * 根据 传输过来的参数 获得指定的小时
	 *
	 * @param currentTime
	 * @param plusMinutes
	 * @return
	 */
	public static String getHourChanges(Date currentTime, int plusHour){
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH");
		Calendar c = new GregorianCalendar();
		c.setTime(currentTime);
		c.add(Calendar.HOUR,plusHour);
		currentTime=c.getTime();
		String changeMinute = df.format(currentTime);
		return changeMinute;
	}
	/**
	 * 根据 传输过来的参数 获得指定的日期
	 *
	 * @param currentTime
	 * @param plusMinutes
	 * @return
	 */
	public static String getDayChanges(Date currentTime, int plusDay){
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c = new GregorianCalendar();
		c.setTime(currentTime);
		c.add(Calendar.DATE,plusDay);
		currentTime=c.getTime();
		String changeMinute = df.format(currentTime);
		return changeMinute;
	}

	/**
	 * 根据 传输过来的时间计算 当前时间与00:00 相差多少分钟
	 *
	 * @param currentTime
	 * @param plusMinutes
	 * @return
	 */
	public static int getMinute(Date currentTime){
		int minute = 0;
		SimpleDateFormat df = new SimpleDateFormat("HH");
		SimpleDateFormat df1 = new SimpleDateFormat("mm");
		minute = Integer.parseInt(df.format(currentTime.getTime()))*60+Integer.parseInt(df1.format(currentTime));
		if(0==minute){
			minute = 1;
		}
		return minute;
	}

	/**
	 * date类型进行格式化输出（返回类型：String）
	 *
	 * @param date
	 * @return yyyy-MM-dd HH:mm:ss
	 */
	public static String dateFormat(Date date) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateString = formatter.format(date);
		return dateString;
	}

	/**
	 * 根据 传输过来的参数 获得指定的分钟
	 *
	 * @param currentTime
	 * @param plusMinutes
	 * @return yyyy-MM-dd HH:mm:ss SSS
	 */
	public static String getMinuteChangesByDate(Date currentTime, int plusMinutes) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar c = new GregorianCalendar();
		c.setTime(currentTime);
		c.add(Calendar.MINUTE, plusMinutes);
		currentTime = c.getTime();
		String changeMinute = df.format(currentTime);
		return changeMinute;
	}

	/**
	 * 根据 传输过来的参数 获得指定的日期
	 *
	 * @param currentTime
	 * @param plusMinutes
	 * @return yyyy-MM-dd HH:mm:ss
	 */
	public static String getDayChangesByDate(Date currentTime, int plusDay) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar c = new GregorianCalendar();
		c.setTime(currentTime);
		c.add(Calendar.DATE, plusDay);
		currentTime = c.getTime();
		String changeMinute = df.format(currentTime);
		return changeMinute;
	}

	public static long timhour2Timestamp(String timehour) throws ParseException {
	    return TIME_HOUR_PARESER.get().parse(timehour).getTime();
    }
}
